var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    news: '',
    scrollTop: 0,
    message: '',
    contentData: [],
    contentData1:[{
      selfName: '姜文飞',
      otherName: '徐静茹',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 19:53',
      isRight: true,
      content: '哈哈哈哈哈',
      isImg: false,
      photoLink: '/images/illness2.png'
    },
    {
      selfName: '姜文飞',
      otherName: '徐静茹2',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: false,
      content: '张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉',
      isImg: false,
      photoLink: '/images/illness2.png'
    },
    {
      selfName: '姜文飞',
      otherName: '张峻宇又在睡觉',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: false,
      content: '张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉张峻宇又在睡觉',
      isImg: true,
      photoLink: '/images/illness2.png'
    },
    {
      selfName: '姜文飞',
      otherName: '张峻宇又在睡觉',
      otherImg: '/images/defaultImage.png',
      time: '2020-12-27 20:43',
      isRight: true,
      content: 'tui',
      isImg: true,
      photoLink: '/images/illness2.png'
    },
    ],
    selfImg: '/images/defaultImage.png',
    otherTel: '',
    otherName: 'hhh',
    timer: ''
  },

  /**
   * 页面加载
   */
  onLoad: function (options) {
    this.setData({
      otherTel: JSON.parse(options.otherTel),
      otherName: JSON.parse(options.otherName),
    });
    console.log(this.data.otherTel)
    var that = this
    wx.request({
      url:  getApp().globalData.url+'getSelfInfo',
      method: 'GET',
      data: {
        username: getApp().globalData.username,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        that.setData({
          selfImg: res.data.logoLink,
        })
      },
      fail(res) {
        wx.showToast({
          title: '获取未读消息失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    });
    this.getContent()
    wx.setNavigationBarTitle({ 
      title: that.data.otherName
    });
  },

  /**
   * 页面显示
   */
  onShow:function(e){
    this.getContent() 
    var that = this 
    that.data.timer = setInterval(function () {
      console.log(that.data.otherTel)
      wx.request({
        url:  getApp().globalData.url + 'doctorGetMessageServe',
        method: 'GET',
        data: {
          tel: getApp().globalData.username,
          otherTel: that.data.otherTel,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          console.log(1)
          if(res.data.length != that.data.oldLength)
          {
            console.log(2)
            that.getContent()
          }
        },
      })
    	console.log("轮播请求10秒触发一次");
    }, 10000); 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //清除计时器  即清除setInter
    clearInterval(this.data.timer)
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setIntertimer
    clearInterval(that.data.timer)
  },

  /**
   * 输入消息
   */
  bindChange: function (e) {
    this.setData({
      message: e.detail.value
    })
  },

  /**
   * 发送消息函数
   */
  send: function (e) {
    var that = this
    if( this.data.message != '' && this.data.message != undefined)
    {
      wx.request({
        url:  getApp().globalData.url+'doctorSendMessageSubmit',
        method: 'POST',
        data: {
          tel: getApp().globalData.username,
          otherTel: that.data.otherTel,
          content: that.data.message,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          if (res.data == "True") {
            that.setData({
              message: ''
            })
            that.getContent();
            that.bottom();
          } 
          else if(res.data=="False"){
            wx.showToast({
              title: '消息发送失败',
              icon: 'none',
            })
            that.getContent();
          }   
        },
        fail(res){
          wx.showToast({
            title: '网络连接中断，消息发送失败',
            icon: 'none',
          })
        }
      })
    }
    else
    {
      wx.showToast({
        title: '您发送的消息不能为空',
        icon: 'none',
      })
    }
  },

  /**
   * 获取聊天记录
   */ 
  getContent: function () {
    var that = this
    wx.request({
      url:  getApp().globalData.url + 'doctorGetMessageServe',
      method: 'GET',
      data: {
        tel: getApp().globalData.username,
        otherTel: that.data.otherTel,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'  // 默认值
      },
      success(res){
        if (res.data != "False" && res.data != "false" && res.data != false) {
          that.setData({
            contentData: res.data,
            oldLength:res.data.length,
          })
        }  
        that.bottom()
      },
      fail(res){
        wx.showToast({
          title: '网络连接中断，请下拉刷新',
          icon: 'none',
        })
      }
    })
  },

  /**
   * 获取hei的id节点然后屏幕焦点调转到这个节点 
   */ 
  bottom: function () {
    var query = wx.createSelectorQuery()
    query.select('#hei').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      wx.pageScrollTo({
        scrollTop: res[0].bottom  // #the-id节点的下边界坐标
      })
      res[1].scrollTop // 显示区域的竖直滚动位置
    })
  },

  /**
   * 选择图片并发送图片 
   */  
  sendImg: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) 
      {
        var tempFilePaths = res.tempFilePaths[0]
        wx.uploadFile({
          url: getApp().globalData.url + 'doctorSendImgSubmit',
          filePath: tempFilePaths,     
          name: 'photoLink', //文件对应的参数名字(key)              
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          formData: {
            tel: getApp().globalData.username,
            otherTel: that.data.otherTel,
          },
          success(res){
            if (res.data == "True") {
              that.getContent()
            } 
            else if(res.data=="False"){
              wx.showToast({
                title: '消息发送失败',
                icon: 'none',
              })
              that.getContent();
            }   
          },
          fail(res){
            wx.showToast({
              title: '网络连接中断，消息发送失败',
              icon: 'none',
            })
          }
        })
      }
    })
  }
})